@file:OptIn(ExperimentalMaterialApi::class)

package com.example.mycomposelesson

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.mycomposelesson.ui.*
import com.example.mycomposelesson.ui.theme.MyComposeLessonTheme
import com.example.mycomposelesson.ui.viewmodel.MainActivityViewModel
import com.example.mycomposelesson.util.SerialUtil
import com.giftedcat.serialportlibrary.listener.OnOpenSerialPortListener
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import mu.KotlinLogging
import java.io.File

class MainActivity : ComponentActivity() {
    private val logger=KotlinLogging.logger(this.javaClass.simpleName)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //initSerialPort()
        setContent {
            LayoutDemoPreview()
//            MyComposeLessonTheme {
//                // A surface container using the 'background' color from the theme
//                Surface(
//                    modifier = Modifier.fillMaxSize(),
//                    color = MaterialTheme.colors.background
//                ) {
//                   // TextPractise()
//                   //BackdropScaffoldDemo()
//                 ControlTest()
// //                   SerialDataListWithVM()
////                    Column(Modifier.fillMaxWidth()) {
////                        SerialDataList()
////                        ClickCounter(clicks =0 ){}
////                    }
//                }
//            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        SerialUtil.closeSerialPort()
    }
}
@Composable
fun GreetingWithSerial(
    viewModel: MainActivityViewModel = viewModel()
){
    val serialdata = viewModel.serialdata.observeAsState()
    serialdata.value?.let {
        MyApplication.logger.debug { "串口数据已变化:${it}" }
        Greeting(it)
    }
}
@Composable
fun Greeting(name: String) {
    Card( modifier = Modifier
        .fillMaxWidth()
        .padding(10.dp) // 外边距
        .clickable{ },
        // 设置点击波纹效果，注意如果 CardDemo() 函数不在 MaterialTheme 下调用
        // 将无法显示波纹效果
        elevation = 10.dp // 设置阴影
              ){
        Text(text = "Hello $name!",
            style = MaterialTheme.typography.h4,
            textAlign= TextAlign.Center
        )
    }

}

@Composable
fun ClickCounter(clicks: Int, onClick: () -> Unit) {
    Button(onClick = onClick) {
        Text("I've been clicked $clicks times")
    }
}

@Composable
fun HelloContent() {
    Column(modifier = Modifier.padding(16.dp)) {
        Text(
            text = "Hello!",
            modifier = Modifier.padding(bottom = 8.dp),
            style = MaterialTheme.typography.h5
        )
        OutlinedTextField(
            value = "",
            onValueChange = { },
            label = { Text("Name") }
        )
    }
}
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    MyComposeLessonTheme {
         Greeting("Android")
       // ClickCounter(clicks =0 ){}
      //  HelloContent()
    }
}
